package com.sky.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.context.BaseContext;
import com.sky.dto.FlyPageQueryDTO;
import com.sky.entity.Fly;
import com.sky.mapper.FlyMapper;
import com.sky.result.PageResult;
import com.sky.service.FlyService;
import com.sky.vo.FlyVO;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

@Service
public class FlyServiceImpl implements FlyService {

    @Autowired
    private FlyMapper flyMapper;
    @Autowired
    private FlyService flyService;

    /**
     * 根据id查询无人机信息
     * @param id
     * @return
     */
    public Fly getById(Long id) {
        Fly fly = flyMapper.getById(id);
        return fly;
    }

    /**
     * 分页查询无人机信息
     * @param flyPageQueryDTO
     * @return
     */
    @Override
    public PageResult pageQuery(FlyPageQueryDTO flyPageQueryDTO) {
        PageHelper.startPage(flyPageQueryDTO.getPage(), flyPageQueryDTO.getPageSize());
        Page<FlyVO> page = flyMapper.pageQuery(flyPageQueryDTO);
        return new PageResult(page.getTotal(), page.getResult());
    }

    /**
     * 更改无人机信息
     * @param flyPageQueryDTO
     * @return
     */
     public void update(FlyPageQueryDTO flyPageQueryDTO) {
         Fly fly = new Fly();
         BeanUtils.copyProperties(flyPageQueryDTO, fly);
         fly.setUpdateUser(getCurrentId());
         fly.setUpdateTime(LocalDateTime.now());
         //修改无人机表基本信息
         flyMapper.update(fly);
         System.out.println("当前操作员id为：" + getCurrentId());
     }

     //获取当前操作员的id
     public Long getCurrentId() {
         Long currentId = BaseContext.getCurrentId();
         return currentId;
     }

    /**
     * 新增无人机
     * @param flyPageQueryDTO
     * @return
     */
    public void save(FlyPageQueryDTO flyPageQueryDTO) {
        Fly fly = new Fly();
        BeanUtils.copyProperties(flyPageQueryDTO, fly);
        fly.setCreateUser(getCurrentId());
        fly.setUpdateUser(getCurrentId());
        fly.setCreateTime(LocalDateTime.now());
        fly.setUpdateTime(LocalDateTime.now());
        flyMapper.insert(fly);
    }

    /**
     * 删除无人机
     * @param id
     * @return
     */
    public void delete(Long id) {
        flyMapper.delete(id);
    }
}
